/**
 * LocationAPITools library, version 1.0.2
 * 
 * WWW: http://code.google.com/p/locationapitools
 *   
 * Summary:
 *   A collection of classes that simplify the development of location-aware mobile
 *   applications using the JSR-179 Location API on the J2ME (CLDC/MIDP) platform,
 *   e.g. on GPS-equipped smartphones.
 * 
 * Author:
 *   Matthias Stevens (Sony CSL Paris / Vrije Universiteit Brussel)
 *   Contact: matthias.stevens@gmail.com
 *  
 * Description:
 *   The primary use case is a location tracker that stores tracks in KML
 *   (cfr. Google Earth/Maps), CSV or plain text format.
 * 
 *   Package "com.sony.csl.j2me.locationapitools.demo" contains a MIDlet class that
 *   demonstrates how LocationAPITools can be used to create simple GPS tracker
 *   application that produces KML files. For examples of more advanced uses of this
 *   library refer to the NoiseTube project (see below, look for "NoiseTube MobileNoiseTagger").
 * 
 *   The code is known to be working on the following Symbian OS-based platforms:
 *     * S60 3rd Edition Feature Pack 1 (Symbian OS 9.2), e.g.: Nokia N95 and Nokia N95 8GB smartphones
 *     * S60 3rd Edition Feature Pack 2 (Symbian OS 9.3) 
 *   Although this is untested, the code should also work on other (non-Symbian/S60)
 *   J2ME CLDC/MIDP platforms/devices, as long as the JSR-179 Location API is supported
 *   on the platform/device in question.
 * 
 *   This library was developed within the scope of the NoiseTube project at
 *   Sony Computer Science Laboratory (CSL) Paris, for more information please refer to:
 *     * http://noisetube.net
 *     * http://code.google.com/p/noisetube
 *     * http://www.csl.sony.fr 
 *    
 * License: 
 *   Copyright 2008-2009 Sony CSL Paris
 *   Licensed under the Apache License, Version 2.0 (the "License"); you may not use
 *   this file except in compliance with the License. You may obtain a copy of the
 *   License at http://www.apache.org/licenses/LICENSE-2.0.
 *   Unless required by applicable law or agreed to in writing, software distributed
 *   under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 *   CONDITIONS OF ANY KIND, either express or implied. See the License for the
 *   specific language governing permissions and limitations under the License.
 */

package com.sony.csl.j2me.locationapitools.net;

import java.io.IOException;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.location.Location;
import javax.microedition.location.LocationProvider;

import com.sony.csl.j2me.locationapitools.LocationProxy;
import com.sony.csl.j2me.locationapitools.LocationProxyListener;
import com.sony.csl.j2me.net.HttpHttpsURL;

/**
 * @author mstevens
 *
 */
public abstract class HttpLocationSubmitter implements LocationProxyListener
{

	private long lastSubmissionTimestamp = -1;
	private int numberOfSubmittedLocations = 0;

	protected abstract int getIntervalSeconds();
	
	protected abstract HttpHttpsURL getURL(Location location);
	
	
	public void reset()
	{
		lastSubmissionTimestamp = -1;
		numberOfSubmittedLocations = 0; 
	}

	/**
	 * @see com.sony.csl.j2me.locationapitools.LocationProxyListener#firstLocationFound(com.sony.csl.j2me.locationapitools.LocationProxy, javax.microedition.location.LocationProvider, javax.microedition.location.Location)
	 */
	public void firstLocationFound(LocationProxy proxy, LocationProvider provider, Location location) { }

	/**
	 * @see com.sony.csl.j2me.locationapitools.LocationProxyListener#locationProviderStateChanged(com.sony.csl.j2me.locationapitools.LocationProxy, javax.microedition.location.LocationProvider, int)
	 */
	public void locationProviderStateChanged(LocationProxy proxy, LocationProvider provider, int newState) { }

	/**
	 * @see com.sony.csl.j2me.locationapitools.LocationProxyListener#locationUpdated(com.sony.csl.j2me.locationapitools.LocationProxy, javax.microedition.location.LocationProvider, javax.microedition.location.Location)
	 */
	public void locationUpdated(LocationProxy proxy, LocationProvider provider, Location location)
	{
		if(location.isValid()) //just to be sure
		{
			if(numberOfSubmittedLocations > 0 || (location.getTimestamp() - lastSubmissionTimestamp >= (getIntervalSeconds() * 1000)))
			{	//submit				
				HttpConnection conn = null;
				String agent = "Mozilla/4.0";
				try
				{
					conn = (HttpConnection) Connector.open(getURL(location).getURIString());
					conn.setRequestMethod(HttpConnection.GET);
					conn.setRequestProperty("User-Agent", agent);
					if(conn.getResponseCode() == HttpConnection.HTTP_OK) //HTTP 200 (successfully submitted)
					{
						lastSubmissionTimestamp = location.getTimestamp();
						numberOfSubmittedLocations++;
					}
				}
				catch(IOException e) { }				
			}
			//else	//interval is not over yet (ignore location)
		}
	}

	/**
	 * @see com.sony.csl.j2me.locationapitools.LocationProxyListener#proxyConnected(com.sony.csl.j2me.locationapitools.LocationProxy, javax.microedition.location.LocationProvider)
	 */
	public void proxyConnected(LocationProxy proxy, LocationProvider provider)
	{ 
		reset();		
	}

	/**
	 * @see com.sony.csl.j2me.locationapitools.LocationProxyListener#proxyDisconnected(com.sony.csl.j2me.locationapitools.LocationProxy)
	 */
	public void proxyDisconnected(LocationProxy proxy) { }

	/**
	 * @see com.sony.csl.j2me.locationapitools.LocationProxyListener#receivesInvalidLocations()
	 */
	public boolean receivesInvalidLocations()
	{
		return false;
	}

}
